<template>
  <view >
    <u-sticky>
      <u-navbar back-text=" " title="竣工验收" :background="background" title-color="#ffffff" :custom-back="customBack" back-icon-color="#ffffff" ref="navbar" title-size="36"></u-navbar>
      <xmInfo
        :projectno="itemContent.projectno"
        :projectname="itemContent.projectname"
        ref="xmInfo"
        class="u-sticky"
      ></xmInfo>
    </u-sticky>
	
    <scroll-view
     class="scroll-list" scroll-y="true" 
	 @scrolltolower="loadMore"
	 :refresher-enabled="true"
	 :refresher-triggered="triggered"
	 @refresherrefresh="refresherrefresh"
	 :style="{ height: computedScrollViewHeight }"
    >
	
  <view
        class="scroll-content"
        ref="scrollContent"
        style="
          display: flex;
          flex-direction: column;
          justify-content: space-between;
        "
		:style="{ height: computedScrollViewHeight }" 
      >
	  
      <view class="cu-item shadow" v-for="(item, index) in list"
          :key="item.id">

          <view
            class="cu-form-group"
            style="display: flex; justify-content: space-between"
          >
            <view
              style="
                display: inline-block;
                padding: 5px;
                background: #3e97b0;
                color: #fff;
                width: 40px;
                text-align: center;
                font-style: italic;
                font-weight: bold;
              "
            >
              {{ index + 1 }}
            </view>
            <view class="text-blod">{{ item.createDate }}</view>
            <!-- -->
            <dictLabel
              :value="item.checkresult ? item.checkresult : 0"
              dict-type="zf_assay_result"
              :dict-list="dictAssayList"
              :fontSize="'30rpx'"
            ></dictLabel>

          </view>

       
        <view class="cu-form-group justify-start">
          <view class="title">发起人</view>
          <view>{{ item.createByName || '' }} {{ "("+item.createBy+")" || ''}}</view>
        </view>
        <!-- <view class="cu-form-group">
          <view class="title">发起人</view>
          <view>{{ item.createByName }}</view>
        </view> -->
		<view class="cu-form-group flex justify-start">
		  <view class="title">验收结果</view>
		  <view >{{ item.checkresult }}</view>
		</view>
        <view class="cu-form-group justify-start">
          <view class="title">不合格描述</view>
          <view>{{ item.remarks }}</view>
        </view>
        <view class="cu-form-group justify-start">
          <view class="title">是否推送报装</view>
          <view>
            <dictLabel
              :value="item.bpushed ? item.bpushed : 0"
              dict-type="sys_yes_no"
              :dict-list="dictBpushedList"
              :fontSize="'30rpx'"
            ></dictLabel>

          </view>
        </view>
        <view class="cu-form-group justify-start">
          <view class="title">状态</view>
          <view>
            <dictLabel
              :value="item.status ? item.status : 0"
              dict-type="zf_jgys_status"
              :dict-list="dictList"
              :fontSize="'30rpx'"
            ></dictLabel> 
          </view>
        </view>

      </view>
      <view v-if="list.length" class="loadmore" style="justify-self: flex-end">
          <u-loadmore :status="loadStatus"></u-loadmore>
        </view>
        <u-empty v-if="!list.length" style="height: 100%;"></u-empty>   
    </view>
    </scroll-view>
	<!-- <view style="height: 80px"></view>
	<view class="btn-group cu-bar foot" style="height: 80px">
	  <button class="cu-btn bg-green shadow-blur" @tap="add()">
	    <text class="cuIcon-add"></text> 发起
	  </button>
	</view> -->
  </view>
</template>
<script>
import xmInfo from "@/pages/zfgs/index/componetns/xmInfo.vue";
import dictLabel from "@/pages/zfgs/index/componetns/dictLabel.vue";
export default {
  components: { xmInfo ,dictLabel},
  data() {
    return {
      list: [],
      modalNameDetail: null,
      applyFile: [],
      showAddDetail: false,
      itemContent: {},
      query: {
        pageNo: 1,
        pageSize: 20,
        // orderBy: "a.create_date desc",
      },
      loadStatus: "loadmore",
      triggered: false,
      dictList: [],
      flag: false,
      scrollViewHeight: 0,
      headerHeight: 0,
      background:{
        backgroundColor: '#3E97B0',
      },
      dictBpushedList:[],
      dictAssayList:[]
    };
  },
  onShow() {
    this.query.pageNo = 1;
    this.loadData();
  },
  onLoad(e) {
    if (typeof e.item !== "undefined") {
      try {
        this.itemContent = JSON.parse(e.item);
        this.query.projectno = this.itemContent.projectno;
        // this.query.projectname = this.itemContent.projectname;
        console.log(this.itemContent);

        // this.loadData();
        this.getDictList();
      } catch (error) {
        console.error("Failed to parse item:", error);
      }
    } else {
      console.warn("Item is undefined.");
    }
  },
  mounted() {
    this.$refs.xmInfo.$on('child-mounted-done', () => {
      this.calculateScrollViewHeight();
    });
  },
  computed: {
    computedScrollViewHeight() {
      return this.scrollViewHeight === 'auto' ? 'auto' : `${this.scrollViewHeight}px`;
    },
  },
  methods: {
	 customBack(){
	   // 跳转到工作台页面
	   // uni.navigateTo({
	   //   url: "/pages/zfgs/index/index/index?item=" + JSON.stringify(this.itemContent),
	   // });
	   uni.navigateBack({
	     delta: 1,
	   });
	 },
	 async calculateScrollViewHeight() {
	   try {
	 // 等待头部高度计算完成
	       await new Promise((resolve) => {
	         this.$nextTick(() => {
	           this.headerHeight = this.$refs.xmInfo.$refs['u-sticky'].height + this.$refs.xmInfo.$refs['u-sticky'].h5NavHeight + this.$refs.navbar.navbarHeight;
	           resolve();
	         });
	       });
	       // 计算tabBar高度
	 //       if(this.status != this.vuex_config.zgStatus){
	 //         this.tabBarHeight = 90;
	 //       }else{
	 //         this.tabBarHeight = 0;
	 //       }
	       this.tabBarHeight = 0
	       // 获取屏幕高度
	       const systemInfo = uni.getSystemInfoSync();
	       const screenHeight = systemInfo.windowHeight;
	       // 计算scroll-view高度
	       this.scrollViewHeight = screenHeight - (this.headerHeight + this.tabBarHeight);
	   } catch (error) {
	     console.error('Error while calculating ScrollView height:', error);
	   }
	 },
    getDictList() {
      this.$u.api.dictListData({ dictType: "zf_jgys_status" }).then((res) => {
        this.dictList = res;
      });
      // sys_yes_no
      this.$u.api.dictListData({ dictType: "sys_yes_no" }).then((res) => {
        this.dictBpushedList = res;
      });
      // zf_assay_result
      this.$u.api.dictListData({ dictType: "zf_assay_result" }).then((res) => {
        this.dictAssayList = res;
      });
    },
    loadMore() {
      this.loadStatus = "loading";
      setTimeout(() => {
        this.query.pageNo += 1;
        this.loadData("add");
      }, 100);
    },
    loadData(type) {
      // 循环10次重复赋值
      this.$u.api.zfgs.pmWatersupplyListData(this.query).then((res) => {
        if (res.list.length >= 0) {
          // this.xmList = res.list
          if (res.list.length < 20 || res.list.length == 0) {
            this.loadStatus = "nomore";
          }
          var data = res.list;

          if (type == "add") {
            for (var i = 0; i < data.length; i++) {
              this.list.push(data[i]);
            }
          } else {
            this.list = data;
          }
        }
      });
    },
    async refresherrefresh() {
      this.triggered = true;
      this.query.pageNo = 1;
      await this.loadData();
      setTimeout(() => {
        this.triggered = false;
      }, 500);
      // this.triggered = false;
    },


    add(item) {
      console.log(item, "item");
      if (item) {
        uni.navigateTo({
          url: "/pages/zfgs/accept/pre/pmPreacceptance/readOnlyForm?id=" + item.id,
        });
      } else {
		  const xm = {
		      projectno : this.itemContent.projectno,
		      name : this.itemContent.projectname,
		  }
        uni.navigateTo({
          url: "/pages/zfgs/accept/pre/pmPreacceptance/form?xm=" + JSON.stringify(xm),
        });
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.cu-bar {
  min-height: 80px;
}
.cu-btn {
  font-size: 16px;
  height: 40px;
}
.foot {
  background: #fff;
}
.sub-title{
  flex-grow: 1; /* 让右侧内容占据剩余空间 */
  max-width: 200px; /* 或其他合适的最大宽度，超过这个宽度时内容会换行 */
  word-break: break-word; /* 或 overflow-wrap: break-word; 允许单词在必要时断开以适应宽度 */
  text-align: right;
}
::v-deep .u-input__input{
    text-align: right;;
}
.cu-form-group {
  flex-wrap: wrap;
}
.cu-item {
  flex-wrap: wrap;
  padding: 15px !important;
  position: relative;
}
.dictLabel {
  position: absolute;
  top: 0;
  right: 0;
}
.title{
  width: 240rpx;
}
.text-blod{
  font-weight: bold;
}
</style>